home *** CD-ROM | disk | FTP | other *** search
/ Mac Easy 2010 May / Mac Life Ubuntu.iso / casper / filesystem.squashfs / usr / lib / python2.6 / dist-packages / AppInstall / widgets / ReleaseNotesViewer.pyc (.txt) < prev    next >
Encoding:
Python Compiled Bytecode  |  2009-10-12  |  6.0 KB  |  171 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 2.6)
  3.  
  4. import pygtk
  5. import gtk
  6. import pango
  7. import subprocess
  8. import os
  9.  
  10. class ReleaseNotesViewer(gtk.TextView):
  11.     
  12.     def __init__(self, notes = None):
  13.         '''Init the ReleaseNotesViewer as an Inheritance of the gtk.TextView.
  14.            Load the notes into the buffer and make links clickable'''
  15.         gtk.TextView.__init__(self)
  16.         self.hovering = False
  17.         self.first = True
  18.         self.set_property('editable', False)
  19.         self.set_cursor_visible(False)
  20.         self.buffer = gtk.TextBuffer()
  21.         self.set_buffer(self.buffer)
  22.         self.connect('event-after', self.event_after)
  23.         self.connect('motion-notify-event', self.motion_notify_event)
  24.         self.connect('visibility-notify-event', self.visibility_notify_event)
  25.         self.buffer.connect_after('insert-text', self.on_insert_text)
  26.         if notes != None:
  27.             self.buffer.set_text(notes)
  28.         
  29.  
  30.     
  31.     def tag_link(self, start, end, url):
  32.         '''Apply the tag that marks links to the specified buffer selection'''
  33.         tag = self.buffer.create_tag(None, foreground = 'blue', underline = pango.UNDERLINE_SINGLE)
  34.         tag.set_data('url', url)
  35.         self.buffer.apply_tag(tag, start, end)
  36.  
  37.     
  38.     def on_insert_text(self, buffer, iter_end, text, *args):
  39.         '''Search for http URLs in newly inserted text  
  40.            and tag them accordingly'''
  41.         iter = buffer.get_iter_at_offset(iter_end.get_offset() - len(text))
  42.         iter_real_end = buffer.get_end_iter()
  43.         for protocol in [
  44.             'http://',
  45.             'https://']:
  46.             while True:
  47.                 ret = iter.forward_search(protocol, gtk.TEXT_SEARCH_VISIBLE_ONLY, iter_end)
  48.                 if not ret:
  49.                     break
  50.                 
  51.                 (match_start, match_end) = ret
  52.                 match_tmp = match_end.copy()
  53.                 while True:
  54.                     if match_tmp.forward_char():
  55.                         text = match_end.get_text(match_tmp)
  56.                         if text in (' ', ')', ']', '\n', '\t', '>', '!'):
  57.                             break
  58.                         
  59.                     else:
  60.                         break
  61.                     match_end = match_tmp.copy()
  62.                 url = match_start.get_text(match_end)
  63.                 tags = match_start.get_tags()
  64.                 tagged = False
  65.                 for tag in tags:
  66.                     url = tag.get_data('url')
  67.                     if url != '':
  68.                         tagged = True
  69.                         break
  70.                         continue
  71.                 
  72.                 if tagged == False:
  73.                     self.tag_link(match_start, match_end, url)
  74.                 
  75.                 iter = match_end
  76.         
  77.  
  78.     
  79.     def event_after(self, text_view, event):
  80.         '''callback for mouse click events'''
  81.         if event.type != gtk.gdk.BUTTON_RELEASE:
  82.             return False
  83.         if event.button != 1:
  84.             return False
  85.         
  86.         try:
  87.             (start, end) = self.buffer.get_selection_bounds()
  88.         except ValueError:
  89.             event.button != 1
  90.             event.button != 1
  91.             event.type != gtk.gdk.BUTTON_RELEASE
  92.         except:
  93.             event.button != 1
  94.  
  95.         if start.get_offset() != end.get_offset():
  96.             return False
  97.         (x, y) = self.window_to_buffer_coords(gtk.TEXT_WINDOW_WIDGET, int(event.x), int(event.y))
  98.         iter = self.get_iter_at_location(x, y)
  99.         tags = iter.get_tags()
  100.         for tag in tags:
  101.             url = tag.get_data('url')
  102.             if url != None:
  103.                 self.open_url(url)
  104.                 break
  105.                 continue
  106.             start.get_offset() != end.get_offset()
  107.         
  108.  
  109.     
  110.     def open_url(self, url):
  111.         '''Open the specified URL in a browser'''
  112.         if os.path.exists('/usr/bin/gnome-open'):
  113.             command = [
  114.                 'gnome-open',
  115.                 url]
  116.         else:
  117.             command = [
  118.                 'x-www-browser',
  119.                 url]
  120.         if os.getuid() == 0 and os.environ.has_key('SUDO_USER'):
  121.             command = [
  122.                 'sudo',
  123.                 '-u',
  124.                 os.environ['SUDO_USER']] + command
  125.         
  126.         subprocess.Popen(command)
  127.  
  128.     
  129.     def motion_notify_event(self, text_view, event):
  130.         '''callback for the mouse movement event, that calls the
  131.            check_hovering method with the mouse postition coordiantes'''
  132.         (x, y) = text_view.window_to_buffer_coords(gtk.TEXT_WINDOW_WIDGET, int(event.x), int(event.y))
  133.         self.check_hovering(x, y)
  134.         self.window.get_pointer()
  135.         return False
  136.  
  137.     
  138.     def visibility_notify_event(self, text_view, event):
  139.         '''callback if the widgets gets visible (e.g. moves to the foreground)
  140.            that calls the check_hovering method with the mouse position
  141.            coordinates'''
  142.         (wx, wy, mod) = text_view.window.get_pointer()
  143.         (bx, by) = text_view.window_to_buffer_coords(gtk.TEXT_WINDOW_WIDGET, wx, wy)
  144.         self.check_hovering(bx, by)
  145.         return False
  146.  
  147.     
  148.     def check_hovering(self, x, y):
  149.         '''Check if the mouse is above a tagged link and if yes show
  150.            a hand cursor'''
  151.         _hovering = False
  152.         iter = self.get_iter_at_location(x, y)
  153.         tags = iter.get_tags()
  154.         for tag in tags:
  155.             url = tag.get_data('url')
  156.             if url != None:
  157.                 _hovering = True
  158.                 break
  159.                 continue
  160.         
  161.         if _hovering != self.hovering or self.first == True:
  162.             self.first = False
  163.             self.hovering = _hovering
  164.             if self.hovering:
  165.                 self.get_window(gtk.TEXT_WINDOW_TEXT).set_cursor(gtk.gdk.Cursor(gtk.gdk.HAND2))
  166.             else:
  167.                 self.get_window(gtk.TEXT_WINDOW_TEXT).set_cursor(gtk.gdk.Cursor(gtk.gdk.LEFT_PTR))
  168.         
  169.  
  170.  
  171.